package com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzxczh.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzxczh.base.dict.service.DictService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.vo.LifelineExcelVo;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import com.css.fxfzxczh.zcpt.sys.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Component
public class LifelineExcelVerifyHandler  implements IExcelVerifyHandler<LifelineExcelVo> {

    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    DictService dictService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(LifelineExcelVo lifelineExcelVo) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");

        if (!PlatformObjectUtils.isEmpty(lifelineExcelVo.getProvince()) && !PlatformObjectUtils.isEmpty(lifelineExcelVo.getCity())  && !PlatformObjectUtils.isEmpty(lifelineExcelVo.getDistrict())) {
            String divisionIds = null;
            try {
                divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(lifelineExcelVo.getProvince(),"utf-8"),URLEncoder.encode(lifelineExcelVo.getCity(),"utf-8"),URLEncoder.encode(lifelineExcelVo.getDistrict(),"utf-8"));
            } catch (Exception e){

            }
            if (PlatformObjectUtils.isEmpty(divisionIds)) {
                result.setSuccess(false);
                message.append("省市区县名称 不符合规范！");
            }
        }else{
            if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getProvince())) {
                result.setSuccess(false);
                message.append("省 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getCity())) {
                result.setSuccess(false);
                message.append("市 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getDistrict())) {
                result.setSuccess(false);
                message.append("区(县) 不能为空！");
            }
        }

        if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getDetailAddress())) {
            result.setSuccess(false);
            message.append("详细地址 不能为空！");
        }

        if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getLifelineName())) {
            result.setSuccess(false);
            message.append("生命线工程名称 不能为空！");
        }

        if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getLongitude())) {
            result.setSuccess(false);
            message.append("经度 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getLatitude())) {
            result.setSuccess(false);
            message.append("纬度 不能为空！");
        }


        if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getLineType())) {
            result.setSuccess(false);
            message.append("生命线类型-一级 不能为空！");
        }else{
            List<String> list = checkData("smxlx");
            if(!list.contains(lifelineExcelVo.getLineType())){
                result.setSuccess(false);
                message.append("生命线类型-一级 不正确！");
            }else{
                if(!lifelineExcelVo.getLineType().equals("生命线工程设备") && PlatformObjectUtils.isEmpty(lifelineExcelVo.getLineType2())){
                    result.setSuccess(false);
                    message.append("生命线类型-二级 不能为空！");
                }else{
                    String lineType = lifelineExcelVo.getLineType();
                    if(lineType.equals("交通系统")){
                        List<String> ejList = checkData("jtxt");
                        if(!ejList.contains(lifelineExcelVo.getLineType2())){
                            result.setSuccess(false);
                            message.append("生命线类型-二级 不正确！");
                        }
                    } else if(lineType.equals("供水系统")){
                        List<String> ejList = checkData("gsxt");
                        if(!ejList.contains(lifelineExcelVo.getLineType2())){
                            result.setSuccess(false);
                            message.append("生命线类型-二级 不正确！");
                        }
                    } else if(lineType.equals("输油系统")){
                        List<String> ejList = checkData("syxt");
                        if(!ejList.contains(lifelineExcelVo.getLineType2())){
                            result.setSuccess(false);
                            message.append("生命线类型-二级 不正确！");
                        }
                    }else if(lineType.equals("燃气系统")){
                        List<String> ejList = checkData("rqxt");
                        if(!ejList.contains(lifelineExcelVo.getLineType2())){
                            result.setSuccess(false);
                            message.append("生命线类型-二级 不正确！");
                        }
                    }else if(lineType.equals("电力系统")){
                        List<String> ejList = checkData("dlxt");
                        if(!ejList.contains(lifelineExcelVo.getLineType2())){
                            result.setSuccess(false);
                            message.append("生命线类型-二级 不正确！");
                        }
                    }else if(lineType.equals("通信系统")){
                        List<String> ejList = checkData("txxt");
                        if(!ejList.contains(lifelineExcelVo.getLineType2())){
                            result.setSuccess(false);
                            message.append("生命线类型-二级 不正确！");
                        }
                    }else if(lineType.equals("水利工程")){
                        List<String> ejList = checkData("slgc");
                        if(!ejList.contains(lifelineExcelVo.getLineType2())){
                            result.setSuccess(false);
                            message.append("生命线类型-二级 不正确！");
                        }
                    }
                }
            }
        }

        if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getDamage())) {
            result.setSuccess(false);
            message.append("破坏情况 不能为空！");
        }else{
            List<String> list = checkData("smxphqk");
            if(!list.contains(lifelineExcelVo.getDamage())){
                result.setSuccess(false);
                message.append("破坏情况 不正确！");
            }
        }

        if (PlatformObjectUtils.isEmpty(lifelineExcelVo.getIntensity())) {
            result.setSuccess(false);
            message.append("推测烈度 不能为空！");
        }else{
            List<String> list = checkData("tcld");
            if(!list.contains(lifelineExcelVo.getIntensity())){
                result.setSuccess(false);
                message.append("推测烈度 不正确！");
            }
        }

        result.setMsg(message.toString());
        return result;
    }

    private List<String> checkData(String code){
        List<DictItemEntity> validDictItemsByDictCode = dictService.getValidDictItemsByDictCode(code);
        List<String> list = new ArrayList<>();
        for(DictItemEntity dictItemEntity: validDictItemsByDictCode){
            String dictItemName = dictItemEntity.getDictItemName();
            list.add(dictItemName);
        }
        return list;
    }


}
